package jerry;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import jerry.html.LogParser;
import jerry.html.data.DataType;
import jerry.html.data.accessors.DataAccessor;
import jerry.html.data.accessors.GameDataAccessor;
import jerry.html.data.accessors.PlayerDBAccessor;
import jerry.html.data.convertors.DataConvertor;
import jerry.html.data.convertors.factory.DataConvertorFactory;
import jerry.html.events.EvtCap;

/**
 * This class generates html files based on the log files passed from the
 * batcher.
 * 
 * @author (h0t@_G0|i
 * 
 */
public class LogToHtmlConvertor {

	/**
	 * This method converts the log file processed to html
	 * 
	 * @param logDir
	 * @param htmlsDir
	 *            object
	 * @param userDBDir
	 */
	public static void generateHtmls(String logFile, File htmlsDir,
			String userDBDir) {
		try {
			LogParser parser = new LogParser();
			Map<String, DataType> gameData = new HashMap<String, DataType>();
			File f = new File(logFile);
			parser.parse(f);
			Map<String, DataAccessor> dataAccessors = new HashMap<String, DataAccessor>();
			dataAccessors.put("game", new GameDataAccessor(userDBDir, f
					.getName()));
			dataAccessors.put("player", new PlayerDBAccessor(userDBDir));
			for (Entry<String, List<EvtCap>> entry : parser.getAllEvents()
					.entrySet()) {
				DataConvertor dataConvertor = DataConvertorFactory
						.getFactoryInstance().getDataConvertor(entry.getKey(),
								dataAccessors);
				if (dataConvertor != null) {
					dataConvertor.setGameData(gameData);
					dataConvertor.convertData(entry.getValue());
				}
			}
			for (Entry<String, DataAccessor> dataAccessor : dataAccessors.entrySet()) {
				DataType toUpdate = gameData.get(dataAccessor.getKey());
				if(toUpdate != null)
					dataAccessor.getValue().updateData(toUpdate);
			}
		} catch (Exception e) {
			System.out.println("Couldn't launch UT ngstats convertor!!");
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		generateHtmls("src/Unreal.ngLog.2010.12.28.20.56.17.7777.log.temp.clear", null, "target/");
	}
}
